import { deleteAction, getAction, postAction, putAction } from '@api/manage'

export const HfOrderDetailMixin = {
  data() {
    return {
      selectValue: '',
      materialData: [],
      editingKey: '',
      selectPagination: { page: 1, pageSize: 8 }, //下拉列表分页
      isSelectLoading: false, //下拉列表是否加载中
      materialField: ['materialCode', 'materialName', 'sap']
    }
  },
  methods: {
    handleAdd() {
      // this.materialData = this.materialData
      this.dataSource.unshift({ add: true, selectable: true, editable: true })
    },
    save(record) {
      if (record.number === null || record.number === undefined) {
        this.$message.error('请输入数量')
        return
      }
      let param = Object.assign({}, record)
      param['sourceId'] = this.mainId
      param['sourceSerial'] = this.sourceSerial
      param['totalPrice'] = param.number * param.taxPrice
      this.loading = false
      if (param.add) {
        postAction(this.url.add, param)
          .then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.loadData()
            } else {
              this.$message.warning(res.message)
            }
          })
          .finally(() => {
            this.loading = true
          })
      } else {
        putAction(this.url.edit, param)
          .then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.loadData()
            } else {
              this.$message.warning(res.message)
            }
          })
          .finally(() => {
            this.loading = true
            this.selectValue = ''
          })
      }
    },
    cancel(record) {
      const newData = [...this.dataSource]
      const target = this.dataSource.find(item => item.id === record.id)
      if (target) {
        target.editable = false
        target.selectable = false
        this.dataSource = newData
        // this.loadData()
        this.selectValue = ''
      }
      if (record.add) {
        this.dataSource.shift()
      }
    },
    edit(record) {
      const newData = [...this.dataSource]
      const target = this.dataSource.find(item => item.id === record.id)
      if (target) {
        target.editable = true
        target.selectable = true
        this.selectValue = target.productCode
        this.dataSource = newData
      }
    },
    searchMaterial(value) {
      let param = {}
      if (this.customId !== undefined) {
        param.customId = this.customId
      }
      if (this.supplierId !== undefined) {
        param.supplierId = this.supplierId
      }
      // 点击搜索框时默认查询所有数据
      if (value === undefined) {
        value = '%'
      }
      param.value = value
      getAction(this.url.materialList, param).then(res => {
        this.materialData = res.result.records
      })
    },
    /**
     * 修改表格的单元格数据
     * @param index 当前数据在filterData中的索引
     * @param record 当前行数据
     */
    changeRecord(index, record) {
      //获取物料信息
      const newData = [...this.dataSource]
      //找到选中行的数据
      const target = this.materialData[index]
      //找到当前行的数据
      const current = newData.find((item, index) => {
        return record.id === item.id
      })
      Object.keys(target).forEach(key => {
        if (target[key] === null) {
          target[key] = ''
        }
      })
      //设置数据
      current.materialId = target.productId
      current.materialCode = target.productCode
      current.materialName = target.productName
      current.price = target.price
      current.taxPrice = target.taxPrice
      current.tax = target.tax
      current.sap = target.sap
      current.unit_dictText = target.unit_dictText
      current.unit = target.unit
      current.materialType_dictText = target.materialType_dictText
      current.materialType = target.materialType
      current.perNumber = target.perNumber
      // current.editable=false
      // current.selectable = false
      this.selectValue = target.productCode
      this.editingKey = ''
      this.dataSource = newData
      // this.materialData = this.materialData
    },
    batchDel: function() {
      if (!this.url.deleteBatch) {
        this.$message.error('请设置url.deleteBatch属性!')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
          console.log('this.selectedRowKeys[a]', this.selectedRowKeys[a])
        }
        var that = this
        this.$confirm({
          title: '确认删除',
          content: '是否删除选中数据?',
          onOk: function() {
            that.loading = true
            deleteAction(that.url.deleteBatch, { ids: ids })
              .then(res => {
                if (res.success) {
                  //重新计算分页问题
                  that.reCalculatePage(that.selectedRowKeys.length)
                  that.$message.success(res.message)
                  that.loadData()
                  that.onClearSelected()
                } else {
                  that.$message.warning(res.message)
                }
              })
              .finally(() => {
                that.loading = false
              })
          }
        })
      }
    },
    clearMaterialData() {
      this.materialData = []
      this.selectPagination.page = 1
    },
    popupScroll({ target: { scrollTop, scrollHeight, clientHeight } }) {
      // 当滚动到底部时，加载更多数据
      if (scrollHeight - scrollTop === clientHeight) {
        this.selectPagination.page++
        this.searchMaterial(this.selectValue)
      }
    }
  }
}
